/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.Timestamp;
import java.util.List;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import utils.Debug;

/**
 *
 * @author daz
 */
public class DataEngine implements IDataEngine {

    private TableDao tableDao;
//    private String host = "202.92.4.22";
    private String host = "localhost";
    private int port = 3306;
    private String databaseUser = "gamepoker";
    private String databasePass = "0901hc";
//    private String databasePass = "GaMe2o12";
    private String databaseSchema = "poker_online";
    private String databaseName = "poker_online";
    private static DataEngine dataEngine;
    public boolean isConnected;

    private DataEngine() {
    }

    public static DataEngine getInstance() {
        if (dataEngine == null) {
            dataEngine = new DataEngine();
            Debug.d("new data engine");

        }
        return dataEngine;
    }

    public static DataEngine getInstance(String host, String dbPort, String user, String pass, String schema) {
        if (dataEngine == null) {
            dataEngine = new DataEngine();
            Debug.d("new data engine");

        }
        return dataEngine;
    }

    @Override
    public void setConfig(String host, int port, String user, String pass, String name, String schema) {
        try {
            System.out.println("host :" + host);
            System.out.println("port :" + port);
            System.out.println("user :" + user);
            System.out.println("name :" + name);
            this.host = host;
            this.port = port;
            this.databaseUser = user;
            this.databasePass = pass;
            this.databaseName = name;
            this.databaseSchema = schema;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void connectToDabatabase() {
        try {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            Class.forName("com.mysql.jdbc.Driver");
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/" + databaseName);
            dataSource.setUsername(databaseUser);
            dataSource.setPassword(databasePass);
            tableDao = new TableDao(dataSource, databaseSchema);
            Debug.d("test:" + tableDao.testConnection());
            isConnected = true;
        } catch (Exception ex) {
            isConnected = false;
        }
    }

    public UserInfo getUserCp(String user) {
        List<UserInfo> list = tableDao.getUserCp(user);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public CompanyInfo getCpInfo(String companyName) {
        List<CompanyInfo> list = tableDao.getSMSInfo(companyName);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public List<CompanyInfo> getCpInfo() {
        List<CompanyInfo> list = tableDao.getSMSInfo();
        if (list.isEmpty()) {
            return null;
        }
        return list;
    }

    public List<CompanyInfo> getVendorInfo() {
        List<CompanyInfo> list = tableDao.getVendorInfo();
        if (list.isEmpty()) {
            return null;
        }
        return list;
    }

    public int testConnection() {
        return this.tableDao.testConnection();
    }

    public void resetUserIsplay() {
        this.tableDao.resetUserIsplay();
    }

    public void changeLoginId(long id, String userName) {
        this.tableDao.changeLoginId(id, userName);
    }

    public void setUserOnline(int state, String userName) {
        this.tableDao.setUserOnline(state, userName);
    }

    public void insertUser(String userName, String pass, String gender, String companyId) {
        this.tableDao.insertUserRegister(userName, pass, gender, companyId);
    }

    public List<UserInfo> findUserName(String userName) {
        return this.tableDao.getUserRegisterInfo(userName);
    }

    public void updateMoney(int moneyChange, String userName) {
        this.tableDao.updateMoney(moneyChange, userName);
    }

    public void updateLevel(String userName, int newLevel) {
        this.tableDao.updateLevel(userName, newLevel);
    }

    public void updateWin(String userName, int newWin) {
        this.tableDao.updateWin(userName, newWin);
    }

    public void updateLose(String userName, int newLose) {
        this.tableDao.updateLose(userName, newLose);
    }

    public void insetNewFriend(String userName1, String userName2) {
        this.tableDao.insertNewFriend(userName1, userName2);
    }

    public void marry(String userName1, String userName2) {
        this.tableDao.marry(userName1, userName2);
    }

    public void insertNewMessenger(String userSend, String userReceive, String message, Timestamp timeSend, int kind) {
        this.tableDao.insertNewMessenger(userSend, userReceive, message, timeSend, kind);
    }

    public List<UserInfo> getTopRich() {
        return this.tableDao.getTopRich();
    }

    public List<UserInfo> getTopLevel() {
        return this.tableDao.getTopLevel();
    }

    public void insertUserStatus(String username, String status) {
        this.tableDao.insertUserStatus(username, status);
    }

    public List<ShopHistoryItem> getUserShopHistory(String name) {
        return this.tableDao.getUserShopHistory(name);
    }

//    public void activateItem(Timestamp newTime, Timestamp oldTime, String userName, int kind) {
//        this.tableDao.activateItem(newTime, oldTime, userName, kind);
//        this.tableDao.insertNewItemHistory(kind, ClientManager.APP_ITEM, userName);
//    }
    public void deleteItemUsed(String name, Timestamp timeUse) {
        this.tableDao.deleteItemUsed(name, timeUse);
    }

    public void marryUser(String userName, String friend) {
        this.tableDao.marryTwoUser(userName, friend);
    }

    public void insertChild(int familyId, String childName, int childGender) {
        this.tableDao.inserChild(familyId, childName, childGender);
    }

    public void deleteMessage(String userSend, String userReceive, Timestamp time) {
        this.tableDao.deleteMessage(userSend, userReceive, time);
    }

    public void setReadMessage(String userSend, String userReceive, Timestamp timeSend) {
        this.tableDao.setReadMessage(userSend, userReceive, timeSend);
    }

    public void setAvatar(int val, String name) {
        this.tableDao.setAvatar(val, name);
    }

    public List<SmsInfo> getSmsQueue() {
        return this.tableDao.getSmsQueue();
    }

    public void logTransaction() {
        this.tableDao.logTransaction();
    }

    public void changePass(String userName, String newPass) {
        this.tableDao.changePass(userName, newPass);
    }

    public void activateAcc(String name) {
        this.tableDao.activateAcc(name);
    }

    public List<UserInfo> getListUser(int number) {
        return this.tableDao.getListUser(number);
    }

    public List<UserInfo> getRobot(int number) {
        return this.tableDao.getRobotUser(number);
    }

    public int getNumberOnline() {
        return this.tableDao.getNumberOnline();
    }

    public int addNewCp(String companyName, String numberActivate, String cpSyntax, String numberCharge1,
            String numberCharge2, String numberCharge3, String cost1, String cost2, String cost3, String coefficient) {
        return this.tableDao.addNewCp(companyName, numberActivate, cpSyntax, numberCharge1,
                numberCharge2, numberCharge3, cost1, cost2, cost3, coefficient);
    }

    public void addManyCp(String companyName, String numberActivate, String cpSyntax, String numberCharge1,
            String numberCharge2, String numberCharge3, String cost1, String cost2, String cost3, String coefficient, int numberVendor) {
        this.tableDao.addManyVendor(companyName, numberActivate, cpSyntax, numberCharge1,
                numberCharge2, numberCharge3, cost1, cost2, cost3, coefficient, numberVendor);
    }

    public void editCp(String companyName, String numberActivate, String cpSyntax, String numberCharge1,
            String numberCharge2, String numberCharge3, String cost1, String cost2, String cost3, String coefficient) {
        this.tableDao.editCp(companyName, numberActivate, cpSyntax, numberCharge1,
                numberCharge2, numberCharge3, cost1, cost2, cost3, coefficient);
    }

    public void deleteCp(String text) {
        this.tableDao.deleteCp(text);
    }

    public int isCpCreated(String companyName) {
        return this.tableDao.isCpCreated(companyName);
    }

    public int countSms(String number, String date, String moth, String year) {
        return this.tableDao.countSms(number, date, moth, year);
    }

    public int counCard(int cardValue, String date, String moth, String year) {
        return this.tableDao.countCard(cardValue, date, moth, year);
    }

    public void updateAdString(String text) {
        this.tableDao.updateAdString(text);
    }
}
